package com.iad.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;


public class PermalinkFilter implements Filter {

  private FilterConfig filterConfig;

  public void init(FilterConfig config) throws ServletException {
    this.filterConfig = config;
  }

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    HttpServletResponse httpResponse = (HttpServletResponse) response;
    String servletPath = httpRequest.getServletPath();
    System.out.println("SERVLET PATH: " + servletPath);
    StringBuffer requestURL = httpRequest.getRequestURL();
    
    if (!servletPath.equals("/messagebroker")  && !servletPath.equals("/UploadExample") && !servletPath.contains(".")) {
      String pathLabel = servletPath.substring(1);
      String target = httpRequest.getContextPath() + "/Main.html#site=" + pathLabel;
      System.out.println("Redirecting to " + target);
      httpResponse.sendRedirect(target);
    }
    else {
      chain.doFilter(request, response);
    }

  }


  public void destroy() {

  }
}
